package user_private_key

import (
	"github.com/xinliangnote/go-gin-api/internal/code"
	"github.com/xinliangnote/go-gin-api/internal/pkg/core"
	"github.com/xinliangnote/go-gin-api/internal/repository/mongodb/user"
	"github.com/xinliangnote/go-gin-api/internal/services/user_private_key"
	"net/http"
)

type listRequest struct {
	UserId         string `form:"userId"`
	PageIndex      uint   `form:"pageIndex"`      // 第几页
	PageSize       uint   `form:"pageSize"`       // 每页显示条数
	ChainName      string `form:"chainName"`      // 每页显示条数
	FeeAddress     string `form:"feeAddress"`     // 每页显示条数
	CollectAddress string `form:"collectAddress"` // 每页显示条数

}

type Page struct {
	Total int64       `json:"total"`
	List  interface{} `json:"list"`
}

func (h *handler) List() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(listRequest)
		res := new(createResponse)

		userInfo := ctx.GetInfo("userInfo").(*user.User)
		if err := ctx.ShouldBindQuery(&req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ParamBindError,
				code.Text(code.ParamBindError)).WithError(err),
			)
			return
		}

		data := new(user_private_key.QueryData)
		data.FeeAddress = req.FeeAddress
		data.ChainName = req.ChainName
		data.CollectAddress = req.CollectAddress
		data.PageIndex = req.PageIndex
		data.PageSize = req.PageSize
		data.UserId = userInfo.Id

		productList, err := h.keyService.List(ctx, data)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.GetUserPrivateKeyListError,
				code.Text(code.GetUserPrivateKeyListError)).WithError(err),
			)
			return
		}

		count, err := h.keyService.Count(ctx, userInfo.Id)

		page := new(Page)
		page.Total = count
		page.List = productList

		res.Code = code.StatusOK
		res.Data = page
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}
